package com.taobao.top
{
	import com.taobao.top.converter.ShopConverter;
	import com.taobao.top.model.Shop;
	import com.taobao.top.resultmodel.ShopUpdateResult;
	public class TOPShop
	{
		public function TOPShop()
		{
		}
		public function getShop(fields:Array,nick:String):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.shop.get"),
			new TopRequestParam("fields",TOPUtil.StringArrayToStringWidthComma(fields)),
			new TopRequestParam("nick",nick)];
			topURLLdr.addEventListener(Event.COMPLETE,shopGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		public function updateShop(title:String = "",bulletin:String = "",desc:String= ""):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.shop.update")];
			if(title!="")
			{
				reqParamArr.push(new TopRequestParam("title",title));
			}
			if(bulletin!="")
			{
				reqParamArr.push(new TopRequestParam("bulletin",bulletin));
			}
			if(desc!="")
			{
				reqParamArr.push(new TopRequestParam("desc",desc));
			}
			topURLLdr.addEventListener(Event.COMPLETE,shopUpdateHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		private function shopGetHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var shop:Shop = shopGetJsonParser(event.target.data);
					dispatchEvent(new TOPEvent(TOPEvent.TOP_DATA,shop));
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function shopGetJsonParser(jsonStr:String):Shop
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				
				return ShopConverter.convert((JSON.decode(jsonStr) as Object).rsp.shop);
			}
			return null;
		}
		
		private function shopUpdateHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var shop:Shop = shopUpdateJsonParser(event.target.data);
					dispatchEvent(new TOPEvent(TOPEvent.TOP_DATA,shop));
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function shopUpdateJsonParser(jsonStr:String):Shop
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				var shopUpdateObj:Object = (JSON.decode(jsonStr) as Object).rsp;
				var shopUpdateResult:ShopUpdateResult = new ShopUpdateResult();
				shopUpdateResult.modified = TOPUtil.getCustomDate(shopUpdateObj.modified.toString());
				shopUpdateResult.sid = shopUpdateObj.sid;
				return shopUpdateResult;
			}
			return null;
		}
		
	}	
}